﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using EAGL.Core;

namespace EAGL.Data
{
    /// <summary>
    /// Builds a feature class in memory according to the supllied fields
    /// </summary>
    public class FeatureClassBuilder
    {
        #region =================================== PROTECTED ===============================
                    
            protected IFields fields;
            protected IFeatureClass featureClass;
            
            protected void buildFeatureClass(esriFeatureType ftype)
            {
                // create in memory workspacae factory
                InMemoryWorkspaceFactory wsf = new InMemoryWorkspaceFactoryClass();
                IWorkspaceName wsn = wsf.Create("", "temp", null, 0);
                IName name = wsn as IName;
                IFeatureWorkspace fws = name.Open() as IFeatureWorkspace;

                // create the feature class 
                IFeatureClass fs = fws.CreateFeatureClass("temp_selection", this.fields, null, null, ftype, "shape", "");
                this.featureClass = fs;
            }
            
        #endregion


        #region =================================== PUBLIC   ================================

        public FeatureClassBuilder(List<IField> fieldList)
        {
            this.fields = GeneralConvertor.ToFields(fieldList);
        }

        public FeatureClassBuilder(IFields fields)
        {
            this.fields = fields;
        }

        /// <summary>
        /// The created featute class
        /// </summary>
        public IFeatureClass FeatureClass
        {
            get 
            {
                this.buildFeatureClass(esriFeatureType.esriFTSimple);
                return this.featureClass;
            }
        }

        #endregion

    }
}
